package com.solo.system.mapper;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.solo.common.core.base.mapper.BasicMapper;
import com.solo.system.api.entity.SysUser;
import com.solo.system.model.user.req.AllocatedUserReq;
import com.solo.system.model.user.req.UnallocatedUserReq;
import com.solo.system.model.user.resp.AllocatedUserResp;
import com.solo.system.model.user.resp.UnallocatedUserResp;
import org.apache.ibatis.annotations.Mapper;

import static com.solo.system.api.entity.table.SysRoleTableDef.SysRoleTable;
import static com.solo.system.api.entity.table.SysUserRoleTableDef.SysUserRoleTable;
import static com.solo.system.api.entity.table.SysUserTableDef.SysUserTable;

/**
 * 用户Mapper
 * @author 十一
 * @since 2023/08/30 17:38
 * 人生若只如初见，何事秋风悲画扇
 **/
@Mapper
public interface SysUserMapper extends BasicMapper<SysUser> {

    /**
     * 按用户名查询单条用户信息
     * @param username 用户名
     * @return {@link SysUser }
     */
    default SysUser selectByUsername(String username) {
        return QueryChain.of(this)
                .where(SysUserTable.Username.eq(username))
                .one();
    }

    /**
     * 按电子邮件查询单条用户信息
     * @param email 电子邮件
     * @return {@link SysUser }
     */
    default SysUser selectByEmail(String email) {
        return QueryChain.of(this)
                .where(SysUserTable.Email.eq(email))
                .one();
    }

    /**
     * 按手机号查询单条用户信息
     * @param mobile 手机号
     * @return {@link SysUser }
     */
    default SysUser selectByMobile(String mobile) {
        return QueryChain.of(this)
                .where(SysUserTable.Mobile.eq(mobile))
                .one();
    }

    default Page<AllocatedUserResp> queryAllocatedUserPage(Page<AllocatedUserResp> page, AllocatedUserReq req) {
        return QueryChain.of(this)
                .select(SysUserTable.AllColumns)
                .from(SysUserTable)
                .leftJoin(SysUserRoleTable).on(SysUserRoleTable.UserId.eq(SysUserTable.UserId))
                .leftJoin(SysRoleTable).on(SysUserRoleTable.RoleId.eq(SysRoleTable.RoleId))
                .where(SysUserRoleTable.RoleId.eq(req.getRoleId())
                        .and(SysUserTable.Username.eq(req.getUsername()))
                        .and(SysUserTable.Mobile.eq(req.getMobile()))
                        .and(SysUserTable.Status.eq(req.getStatus())))
                .pageAs(page, AllocatedUserResp.class);
    }

    default Page<UnallocatedUserResp> queryUnallocatedUserPage(Page<UnallocatedUserResp> page, UnallocatedUserReq req) {
        return QueryChain.of(this)
                .select(SysUserTable.AllColumns)
                .from(SysUserTable)
                .leftJoin(SysUserRoleTable).on(SysUserRoleTable.UserId.eq(SysUserTable.UserId).and(SysUserRoleTable.RoleId.eq(req.getRoleId())))
                .leftJoin(SysRoleTable).on(SysUserRoleTable.RoleId.eq(SysRoleTable.RoleId))
                .where(SysUserRoleTable.RoleId.isNull())
                .pageAs(page, UnallocatedUserResp.class);
    }
}
